﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PTTK_HTTT_06.BUS;

namespace PTTK_HTTT_06.GUI.LenThucDon
{
    public partial class MHDanhSachYeuCauNguyenLieu : UserControl
    {
        //
        public THUC_DON thucDon { get; set; }
        public List<CT_THUC_DON> ctThucDonList { get; set; }

        private bool isFirstPage = true;
        private int curRow = 0;
        private int PosX = 0;
        private int PosY = 0;

        public MHDanhSachYeuCauNguyenLieu()
        {
            InitializeComponent();
        }


        #region MHDanhSachYeuCauNguyenLieu_Load()
        private void MHDanhSachYeuCauNguyenLieu_Load(object sender, EventArgs e)
        {
            Dictionary<DateTime, Dictionary<int, double>> dsNguyenLieuTheoNgay = new Dictionary<DateTime, Dictionary<int, double>>();

            //lấy nguyên liệu theo ngày
            for (DateTime i = thucDon.NgayBatDau; i.Date <= thucDon.NgayKetThuc.Date; i = i.AddDays(1))
            {
                //// lọc lấy chi tiết thực đơn theo ngày
                List<CT_THUC_DON> tempCTThucDonList = ctThucDonList.Where(a => a.Ngay.Date == i.Date).ToList();

                //Biến lưu giữ nguyên liệu
                Dictionary<int, double> tempNguyenLieuList = new Dictionary<int, double>();

                //lấy công thức theo mã món ăn
                foreach (CT_THUC_DON cttd in tempCTThucDonList)
                {
                    //lấy công thức
                    List<CONG_THUC> tempCongThucList = CONG_THUC.LayCongThuc(cttd.MaMonAn);

                    //thêm vào danh sách nguyên liệu theo ngày
                    foreach(CONG_THUC congThuc in tempCongThucList){
                        //kiểm tra nguyên liệu theo ngày đã có trong danh sach chua
                        if (tempNguyenLieuList.Keys.Contains(congThuc.MaNguyenLieu))
                        {
                            //cập nhật thêm số lượng
                            tempNguyenLieuList[congThuc.MaNguyenLieu] += ((double)congThuc.SoLuong * (double)cttd.SoLuong); 
                        }
                        else
                        {
                            //thêm vào danh sách
                            tempNguyenLieuList.Add(congThuc.MaNguyenLieu, (double)congThuc.SoLuong * (double)cttd.SoLuong);
                        }
                    }
                }


                //thêm vào danh sách nguyên liệu theo ngày
                if (tempNguyenLieuList.Count > 0)
                {
                    dsNguyenLieuTheoNgay.Add(i.Date, tempNguyenLieuList);
                }
            }
            

            //lấy danh sách nguyên liệu & đơn vị tính
            List<NGUYEN_LIEU> nguyenLieuList = NGUYEN_LIEU.LayDanhSach();
            List<DON_VI_TINH> donViTinhList = DON_VI_TINH.LayDanhSach();

            //Xử lý hiển thị
            lblNgay.Text = "Từ ngày " + thucDon.NgayBatDau.ToString("dd/MM/yyy") + " đến " + thucDon.NgayKetThuc.ToString("dd/MM/yyy");

            int numRow = 0;
            foreach (KeyValuePair<DateTime, Dictionary<int, double>> iDic in dsNguyenLieuTheoNgay)
            {
                numRow++;
                gViewDanhSach.Rows.Add("",iDic.Key.ToString("dd/MM/yyy"),"","");
                gViewDanhSach.Rows[numRow - 1].Cells[1].Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
                gViewDanhSach.Height = gViewDanhSach.Height + gViewDanhSach.Rows[numRow - 1].Height;

                int numNL = 0;
                foreach (KeyValuePair<int, double> nl in iDic.Value)
                {
                    numRow++;
                    numNL++;
                    NGUYEN_LIEU nguyenLieu = nguyenLieuList.Single(a => a.MaNguyenLieu == nl.Key);
                    double totalNL = Math.Round(nl.Value, 0, MidpointRounding.AwayFromZero);
                    if (totalNL < nl.Value)
                        totalNL++;

                    gViewDanhSach.Rows.Add(numNL, nguyenLieu.TenNguyenLieu, Math.Round(totalNL, 0), donViTinhList.Single(a => a.MaDonViTinh == nguyenLieu.MaDonViTinh).TenDonViTinh);
                    gViewDanhSach.Height = gViewDanhSach.Height + gViewDanhSach.Rows[numRow - 1].Height;
                }
            }

            gViewDanhSach.Refresh();
        }
        #endregion
        
        #region InDanhSach()
        public void InDanhSach()
        {
            //Open the print dialog
            System.Drawing.Printing.PrintDocument printDocument = new System.Drawing.Printing.PrintDocument();
            PrintDialog printDialog = new PrintDialog();
            printDialog.Document = printDocument;
            printDialog.UseEXDialog = true;
            //Get the document
            if (DialogResult.OK == printDialog.ShowDialog())
            {
                printDocument.DocumentName = "ds yeu cau nguyen lieu";
                printDocument.PrintPage += printDocument_PrintPage;
                
                PrintPreviewDialog pp = new PrintPreviewDialog();
                pp.Document = printDocument;
                pp.ShowDialog();
            }
        }

        #region print page
        void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            try
            {
                //Set the top margin
                int marginTopAndBottom = 30;
                Graphics graphics = e.Graphics;

                Font font = SystemFonts.MessageBoxFont;
                Rectangle rectangle = e.PageBounds;
                RectangleF recRow = new RectangleF(rectangle.Location, rectangle.Size);
                recRow.Y = marginTopAndBottom;

                StringFormat stringFormatCenter = new StringFormat();
                stringFormatCenter.LineAlignment = StringAlignment.Center;
                stringFormatCenter.Alignment = StringAlignment.Center;

                StringFormat stringFormatRight = new StringFormat();
                stringFormatRight.LineAlignment = StringAlignment.Center;
                stringFormatRight.Alignment = StringAlignment.Far;

                StringFormat stringFormatLeft = new StringFormat();
                stringFormatRight.LineAlignment = StringAlignment.Near;
                stringFormatRight.Alignment = StringAlignment.Near;

                if (isFirstPage)
                {
                    //ghi tiêu đề
                    #region ghi tiêu đề
                    recRow.Height = lblTieuDe.Height;
                    graphics.DrawString(lblTieuDe.Text, lblTieuDe.Font, new SolidBrush(Color.Black), recRow, stringFormatCenter);
                    recRow.Y += lblTieuDe.Height;

                    recRow.Height = lblNgay.Height;
                    graphics.DrawString(lblNgay.Text, lblNgay.Font, new SolidBrush(Color.Black), recRow, stringFormatCenter);
                    recRow.Y += lblNgay.Height;
                    #endregion
                }

                //khởi tạo biến xử lý PosX, PosY
                #region khởi tạo col_STT, col_TenNguyenLieu, col_SoLuong, col_DonVi
                PosX = (e.PageBounds.Width - gViewDanhSach.Width)/2;
                PosY = (int)recRow.Y + 20;

                Rectangle col_STT = new Rectangle(PosX, PosY, gViewDanhSach_col_STT.Width, 40);
                PosX += gViewDanhSach_col_STT.Width;

                Rectangle col_TenNguyenLieu = new Rectangle(PosX, PosY, gViewDanhSach_col_TenNguyenLieu.Width, 40);
                PosX += gViewDanhSach_col_TenNguyenLieu.Width;

                Rectangle col_SoLuong = new Rectangle(PosX, PosY, gViewDanhSach_col_SoLuong.Width, 40);
                PosX += gViewDanhSach_col_SoLuong.Width;

                Rectangle col_DonVi = new Rectangle(PosX, PosY, gViewDanhSach_col_DonVi.Width, 40);
                #endregion

                //ghi header column
                #region ghi header column
                graphics.DrawRectangle(Pens.Black, col_STT);
                graphics.DrawString("STT", gViewDanhSach.ColumnHeadersDefaultCellStyle.Font, Brushes.Black, col_STT, stringFormatCenter);

                graphics.DrawRectangle(Pens.Black, col_TenNguyenLieu);
                graphics.DrawString("Tên Nguyên Liệu", gViewDanhSach.ColumnHeadersDefaultCellStyle.Font, Brushes.Black, col_TenNguyenLieu, stringFormatCenter);

                graphics.DrawRectangle(Pens.Black, col_SoLuong);
                graphics.DrawString("Số Lượng", gViewDanhSach.ColumnHeadersDefaultCellStyle.Font, Brushes.Black, col_SoLuong, stringFormatCenter);

                graphics.DrawRectangle(Pens.Black, col_DonVi);
                graphics.DrawString("Đơn Vị", gViewDanhSach.ColumnHeadersDefaultCellStyle.Font, Brushes.Black, col_DonVi, stringFormatCenter);

                #region cài đặt PosY
                col_STT.Y += col_STT.Height;
                col_TenNguyenLieu.Y += col_TenNguyenLieu.Height;
                col_SoLuong.Y += col_SoLuong.Height;
                col_DonVi.Y += col_DonVi.Height;
                #endregion
                #endregion

                //ghi các dòng
                #region ghi các dòng
                #region cài đặt PosY height
                col_STT.Height = gViewDanhSach.Rows[0].Height;
                col_TenNguyenLieu.Height = gViewDanhSach.Rows[0].Height;
                col_SoLuong.Height = gViewDanhSach.Rows[0].Height;
                col_DonVi.Height = gViewDanhSach.Rows[0].Height;
                #endregion

                for (int i = curRow; i < gViewDanhSach.Rows.Count; i++, curRow++)
                {
                    graphics.DrawRectangle(Pens.Black, col_STT);
                    graphics.DrawString(gViewDanhSach.Rows[i].Cells[0].Value.ToString(), font, Brushes.Black, col_STT, stringFormatCenter);

                    graphics.DrawRectangle(Pens.Black, col_TenNguyenLieu);
                    graphics.DrawString(gViewDanhSach.Rows[i].Cells[1].Value.ToString(), font, Brushes.Black, col_TenNguyenLieu, stringFormatCenter);

                    graphics.DrawRectangle(Pens.Black, col_SoLuong);
                    graphics.DrawString(gViewDanhSach.Rows[i].Cells[2].Value.ToString(), font, Brushes.Black, col_SoLuong, stringFormatCenter);

                    graphics.DrawRectangle(Pens.Black, col_DonVi);
                    graphics.DrawString(gViewDanhSach.Rows[i].Cells[3].Value.ToString(), font, Brushes.Black, col_DonVi, stringFormatCenter);

                    #region cài đặt PosY
                    col_STT.Y += col_STT.Height;
                    col_TenNguyenLieu.Y += col_TenNguyenLieu.Height;
                    col_SoLuong.Y += col_SoLuong.Height;
                    col_DonVi.Y += col_DonVi.Height;
                    #endregion

                    if ((col_STT.Y + col_STT.Height) > (e.PageBounds.Height - marginTopAndBottom * 2))
                    {
                        isFirstPage = false;
                        e.HasMorePages = true;
                        break;
                    }
                }
                #endregion


            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #endregion
    }
}
